import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    //最长数对链
    public int findLongestChain(int[][] pairs) {
        // 预处理
        Arrays.sort(pairs, (a, b) -> a[0] - b[0]);
        int n = pairs.length;
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
        }
        int ret = 1;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (pairs[j][1] < pairs[i][0]) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
                if (dp[i] > ret) {
                    ret = dp[i];
                }
            }
        }
        return ret;
    }
    //最长定差子序列
    public int longestSubsequence(int[] arr, int difference) {
        //创建一个hash表，在hash表中做动态规划
        Map<Integer,Integer> hash = new HashMap<Integer,Integer>();//arr[i] dp[i]
        int ret = 1;
        for(int a : arr) {
            //遍历时会覆盖之前的值
            hash.put(a, hash.getOrDefault(a - difference,0) + 1);
            ret = Math.max(hash.get(a),ret);
        }
        return ret;
    }
}
